package com.zmx.patients.fragment;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.gyf.immersionbar.ImmersionBar;
import com.zmx.patients.MyApplication;
import com.zmx.patients.R;
import com.zmx.patients.okhttp.OkHttp3ClientManager;
import com.zmx.patients.utils.AllUtil;
import com.zmx.patients.view.activity.AboutUsActivity;
import com.zmx.patients.view.activity.ArticleCollectsActivity;
import com.zmx.patients.view.activity.BraceletDataActivity;
import com.zmx.patients.view.activity.BraceletInformationActivity;
import com.zmx.patients.view.activity.CollectionDoctorActivity;
import com.zmx.patients.view.activity.ExpertConsultationActivity;
import com.zmx.patients.view.activity.FeedbackActivity;
import com.zmx.patients.view.activity.LoginActivity;
import com.zmx.patients.view.activity.MeBangleActivity;
import com.zmx.patients.view.activity.MePhysicianActivity;
import com.zmx.patients.view.activity.MedicationDiaryActivity;
import com.zmx.patients.view.activity.MessageActivity;
import com.zmx.patients.view.activity.MyOrderActivity;
import com.zmx.patients.view.activity.MyPrescriptionActivity;
import com.zmx.patients.view.activity.OrderManagementActivity;
import com.zmx.patients.view.activity.PatientMessageActivity;
import com.zmx.patients.view.activity.PersonalDataActivity;
import com.zmx.patients.view.activity.SetUpActivity;
import com.zmx.patients.view.util.MySharedPreferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;


public class MineFragment extends SimpleImmersionFragment implements View.OnClickListener {

    private RelativeLayout relative1, relative2, relative3,relative4;
    private RelativeLayout my_order1, my_order2, my_order3, my_order5;
    private RelativeLayout relativeLayout1, relativeLayout2, relativeLayout3, relativeLayout4, relativeLayout5, relativeLayout6, relativeLayout7, relativeLayout8;
    private TextView login_registered;

    private CircleImageView image_head;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);

        login_registered = view.findViewById(R.id.login_registered);
        login_registered.setOnClickListener(this);
        image_head = view.findViewById(R.id.image_head);
        relative1 = view.findViewById(R.id.relative1);
        relative1.setOnClickListener(this);
        relative2 = view.findViewById(R.id.relative2);
        relative2.setOnClickListener(this);
        relative3 = view.findViewById(R.id.relative3);
        relative3.setOnClickListener(this);
        relative4 = view.findViewById(R.id.relative4);
        relative4.setOnClickListener(this);

        my_order1 = view.findViewById(R.id.my_order1);
        my_order1.setOnClickListener(this);
        my_order2 = view.findViewById(R.id.my_order2);
        my_order2.setOnClickListener(this);
        my_order3 = view.findViewById(R.id.my_order3);
        my_order3.setOnClickListener(this);
        my_order5 = view.findViewById(R.id.my_order5);
        my_order5.setOnClickListener(this);

        relativeLayout1 = view.findViewById(R.id.relativeLayout1);
        relativeLayout1.setOnClickListener(this);
        relativeLayout2 = view.findViewById(R.id.relativeLayout2);
        relativeLayout2.setOnClickListener(this);
        relativeLayout3 = view.findViewById(R.id.relativeLayout3);
        relativeLayout3.setOnClickListener(this);
        relativeLayout4 = view.findViewById(R.id.relativeLayout4);
        relativeLayout4.setOnClickListener(this);
        relativeLayout5 = view.findViewById(R.id.relativeLayout5);
        relativeLayout5.setOnClickListener(this);
        relativeLayout6 = view.findViewById(R.id.relativeLayout6);
        relativeLayout6.setOnClickListener(this);
        relativeLayout7 = view.findViewById(R.id.relativeLayout7);
        relativeLayout7.setOnClickListener(this);
        relativeLayout8 = view.findViewById(R.id.relativeLayout8);
        relativeLayout8.setOnClickListener(this);
        return view;
    }


    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this).titleBarMarginTop(R.id.toolbar).statusBarDarkFont(true).init();
        getUserMessage();
    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent();

        switch (v.getId()) {

            case R.id.login_registered:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                }

                break;

            case R.id.relative1:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), MePhysicianActivity.class);
                    startActivity(intent);
                }

                break;

            case R.id.relative2:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), ExpertConsultationActivity.class);
                    startActivity(intent);

                }

                break;

            case R.id.relative3:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), CollectionDoctorActivity.class);
                    startActivity(intent);

                }
                break;

            case R.id.relative4:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    getPulesStatus();


                }
                break;

            case R.id.my_order1:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), OrderManagementActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("logo", "1");
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
                break;
            case R.id.my_order2:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), OrderManagementActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("logo", "2");
                    intent.putExtras(bundle);
                    startActivity(intent);

                }
                break;
            case R.id.my_order3:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), OrderManagementActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("logo", "3");
                    intent.putExtras(bundle);
                    startActivity(intent);

                }
                break;

            case R.id.my_order5:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), OrderManagementActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("logo", "0");
                    intent.putExtras(bundle);
                    startActivity(intent);

                }
                break;
            case R.id.relativeLayout1:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), MessageActivity.class);
                    startActivity(intent);
                }


                break;
            case R.id.relativeLayout2:


                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), ArticleCollectsActivity.class);
                    startActivity(intent);
                }


                break;
            case R.id.relativeLayout3:


                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), MyPrescriptionActivity.class);
                    startActivity(intent);
                }


                break;
            case R.id.relativeLayout4:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), MedicationDiaryActivity.class);
                    startActivity(intent);
                }


                break;
            case R.id.relativeLayout5:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {
                    intent.setClass(MineFragment.this.getContext(), PersonalDataActivity.class);
                    startActivity(intent);
                }

                break;
            case R.id.relativeLayout6:
                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), SetUpActivity.class);
                    startActivity(intent);

                }

                break;
            case R.id.relativeLayout7:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), FeedbackActivity.class);
                    startActivity(intent);

                }

                break;
            case R.id.relativeLayout8:

                if (!MyApplication.isLogin_state()) {

                    intent.setClass(MineFragment.this.getContext(), LoginActivity.class);
                    startActivityForResult(intent, 10);

                } else {

                    intent.setClass(MineFragment.this.getContext(), AboutUsActivity.class);
                    startActivity(intent);

                }

                break;

        }

    }


    //获取用户资料
    public void getUserMessage() {

        //判断是否登录了
        String token = MySharedPreferences.getInstance(this.getActivity()).getString(MySharedPreferences.token, "");

        if (AllUtil.isNotNull(token)) {//已经登录
            login_registered.setText(MySharedPreferences.getInstance(this.getActivity()).getString(MySharedPreferences.name, ""));
            Glide.with(getActivity()).load(MySharedPreferences.getInstance(this.getActivity()).getString(MySharedPreferences.avatar, "")).into(image_head);
        }


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_CANCELED) {
            return;
        }

        switch (requestCode) {

            case 10:

                Toast.makeText(this.getActivity(), "成功登录", Toast.LENGTH_LONG).show();
                getUserMessage();

                break;

            case 0:

                getUserMessage();

                break;

        }

    }


    @Override
    public void onResume() {
        super.onResume();

        if (!MyApplication.isLogin_state()) {

            login_registered.setText("注册/登录");
            Glide.with(getActivity()).load("http://zmx.123xk.cn/static/upload/member/default.png").into(image_head);

        }

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case 1:

                    try {

                        JSONObject jsonObject = new JSONObject(msg.obj.toString());

                        if (jsonObject.getInt("code") == 1) {

                            Intent intent = new Intent();
                            intent.setClass(MineFragment.this.getContext(), BraceletInformationActivity.class);
                            intent.putExtra("p_id", MySharedPreferences.getInstance(MineFragment.this.getContext()).getString(MySharedPreferences.uid, ""));
                            startActivity(intent);

                        } else {

                            Toast.makeText(MineFragment.this.getContext(), jsonObject.getString("msg"), Toast.LENGTH_LONG).show();

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    break;

            }

        }
    };

    /**
     * 检测脉象
     */
    public void getPulesStatus () {

        List<String> ls = new ArrayList<>();
        ls.add("5");
        ls.add(MySharedPreferences.getInstance(MineFragment.this.getContext()).getString(MySharedPreferences.uid, ""));
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(MineFragment.this.getContext()).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(MineFragment.this.getContext()).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getPulesStatus");
        params.put("data[patient_id]", MySharedPreferences.getInstance(MineFragment.this.getContext()).getString(MySharedPreferences.uid, ""));
        params.put("data[minute]", "5");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/watch", params, handler, 1, 404);

    }


    //签名
    public String sing(List<String> list) {

        StringBuilder requestparams = new StringBuilder();
        requestparams.append(MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.uid, "") +
                MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.role, "") +
                System.currentTimeMillis() / 1000 + MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.password, ""));

        if (list != null) {
            for (String s : list) {
                requestparams.append(s);
            }
        }
        return requestparams.toString();

    }


}